﻿using System;
using System.Windows.Controls;
using System.Collections.Generic;
using System.Windows;

namespace WickedFlame.GenDock
{
    public class CollapsibleAnchor : Anchor
    {
        

        public CollapsibleAnchor()
            : base()
        {
            this.SizeChanged += new System.Windows.SizeChangedEventHandler(OnSizeChanged);
        }

        #region Anchor Member

        public override void AddContent(ContentDocument content)
        {
            if (content.MinWidth == 0)
                content.MinWidth = content.Width;
            //throw new NotImplementedException();
             var btn = new CollapsibleAnchorButton();
            btn.Width = content.Width;
            btn.Height = 20;
            btn.Icon = content.Icon;
            btn.Title = content.Title;

            btn.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(OnPropertyChanged);

            AddContent(btn, content);
            //listContents.Add(btn, content);

            VisibleChildren.Add(btn);
            VisibleChildren.Add(content);

            btn.IsSelected = false;
            InvalidateContent();

            ResizeControls();

            if (OpenNewItems)
                btn.IsSelected = true;
        }

        public override void AddContent(ContentDocument content, Point point)
        {
            //TODO:
            //Not jet Implemented!
            //this is only a copy of  AddContent(ContentDocument content)!!!!

            if (content.MinWidth == 0)
                content.MinWidth = content.Width;
            
            var btn = new CollapsibleAnchorButton();
            btn.Width = content.Width;
            btn.Height = 20;
            btn.Icon = content.Icon;
            btn.Title = content.Title;

            btn.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(OnPropertyChanged);

            AddContent(btn, content);

            VisibleChildren.Add(btn);
            VisibleChildren.Add(content);

            btn.IsSelected = false;
            InvalidateContent();

            ResizeControls();

            if (OpenNewItems)
                btn.IsSelected = true;
        }

        public override void RemoveContent(ContentDocument content)
        {
            //throw new NotImplementedException();
            
            foreach (CollapsibleAnchorButton button in listContents.Keys)
            {
                if (listContents[button] == content)
                {
                    this.VisibleChildren.Remove(listContents[button]);
                    this.VisibleChildren.Remove(button);
                    this.listContents.Remove(button);
                    
                    this.InvalidateContent();
                    break;
                }
            }

            ResizeControls();
        }

        public override bool ShowContent(Type type)
        {
            foreach (CollapsibleAnchorButton button in listContents.Keys)
            {
                Type t = listContents[button].GetType();
                if (t == type)
                {
                    button.IsSelected = true;
                    //OnClicked(button, new RoutedEventArgs());
                    return true;
                }
            }

            return false;
        }

        public override bool ShowContent(ContentDocument content)
        {
            foreach (CollapsibleAnchorButton button in listContents.Keys)
            {
                if (listContents[button] == content)
                {
                    button.IsSelected = true;
                    //OnClicked(button, new RoutedEventArgs());
                    return true;
                }
            }

            return false;
        }

        #endregion

        private void OnSizeChanged(object sender, SizeChangedEventArgs e)
        {
        }

        private void OnPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "IsSelected":
                    try
                    {
                        if ((sender as ButtonBase).IsSelected)
                            listContents[sender as ButtonBase].Visibility = System.Windows.Visibility.Visible;
                        else
                            listContents[sender as ButtonBase].Visibility = System.Windows.Visibility.Collapsed;
                    }
                    catch { }
                    break;
            }
        }

        private void ResizeControls()
        {
            double width = 0.0;

            foreach (FrameworkElement child in VisibleChildren)
            {
                if (child.MinWidth > width)
                    width = child.MinWidth;
            }

            foreach (FrameworkElement child in VisibleChildren)
            {
                if (child is CollapsibleAnchorButton)
                    child.Width = width;
            }
        }

        public bool OpenNewItems
        {
            get
            {
                return (bool)GetValue(OpenNewItemsProperty);
            }
            set
            {
                SetValue(OpenNewItemsProperty, value);
            }
        }

        public static readonly DependencyProperty OpenNewItemsProperty =
            DependencyProperty.Register("OpenNewItems", typeof(bool), typeof(ButtonBase), new PropertyMetadata(true));
    }
}
